﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Open.Domain.Abstractions;
using Open.Domain.Abstractions.Implements;

namespace Open.Repository.EntityFramework
{
    public static class ServiceCollectionExtensions
    {

        /// <summary>
        /// 注册工作单元服务
        /// </summary>
        /// <typeparam name="TContext">工作单元上下文类型</typeparam>
        /// <param name="builder">服务集合</param>
        public static IServiceCollection AddUnitOfWork<TContext>(this IServiceCollection builder)
        where TContext : DbContext
        {
            var context = builder.BuildServiceProvider().GetService<TContext>();
            builder.TryAddSingleton<IUnitOfWork>(x => new UnitOfWork(context));
            return builder;
        }
    }
}
